
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.RC;
using Apps.Models.RC;
using Apps.IBLL.RC;
using System.Data.Common;
using System.Data;

namespace Apps.BLL.RC
{
    public partial class RC_WEIGHINGHCBLL : Virtual_RC_WEIGHINGHCBLL, IRC_WEIGHINGHCBLL
    {


    }
    public class Virtual_RC_WEIGHINGHCBLL
    {
        [Dependency]
        public IRC_WEIGHINGHCRepository m_Rep { get; set; }

        public virtual List<V_GDH_WEIGHING> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<V_GDH_WEIGHING> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                                a => a.WEIGHING_ID.Contains(queryStr)
                                || a.CAR_NUM.Contains(queryStr)
                                );
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public virtual List<V_GDH_WEIGHING> GetPagerList(ref GridPager pager, Expression<Func<V_GDH_WEIGHING, bool>> whereLambda)
        {

            IQueryable<V_GDH_WEIGHING> queryData = m_Rep.GetList(whereLambda);

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public virtual List<V_GDH_WEIGHING> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            return new List<V_GDH_WEIGHING>();
        }

        public virtual List<V_GDH_WEIGHING> GetList(Expression<Func<V_GDH_WEIGHING, bool>> whereLambda)
        {

            IQueryable<V_GDH_WEIGHING> queryData = m_Rep.GetList(whereLambda);

            return CreateModelList(ref queryData);
        }

        public virtual List<V_GDH_WEIGHING> CreateModelList(ref IQueryable<V_GDH_WEIGHING> queryData)
        {

            List<V_GDH_WEIGHING> modelList = queryData.ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, V_GDH_WEIGHING model)
        {
            try
            {
                V_GDH_WEIGHING entity = m_Rep.GetById(model.WEIGHING_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new V_GDH_WEIGHING();
                entity.WEIGHING_ID = model.WEIGHING_ID;
                entity.SERIAL_NUM = model.SERIAL_NUM;
                entity.CAR_NUM = model.CAR_NUM;
                entity.CAR_TYPE = model.CAR_TYPE;
                entity.GROSS = model.GROSS;
                entity.GROSS_TIME = model.GROSS_TIME;
                entity.TARE = model.TARE;
                entity.TARE_TIME = model.TARE_TIME;
                entity.NET = model.NET;
                entity.BILL = model.BILL;
                entity.LOSS = model.LOSS;
                entity.TIME = model.TIME;
                entity.TRAIN_NUM = model.TRAIN_NUM;
                entity.USER_ID = model.USER_ID;
                entity.STATUS = model.STATUS;
                entity.STATE = model.STATE;


                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }


        public virtual bool Deletes(ref ValidationErrors errors, Expression<Func<V_GDH_WEIGHING, bool>> whereLambda)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {

                    foreach (var entity in db.V_GDH_WEIGHING.Where(whereLambda))
                    {
                        db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                    }

                    if (db.SaveChanges() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }




        public virtual bool Edit(ref ValidationErrors errors, V_GDH_WEIGHING model)
        {
            try
            {
                V_GDH_WEIGHING entity = m_Rep.GetById(model.WEIGHING_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.WEIGHING_ID = model.WEIGHING_ID;
                entity.SERIAL_NUM = model.SERIAL_NUM;
                entity.CAR_NUM = model.CAR_NUM;
                entity.CAR_TYPE = model.CAR_TYPE;
                entity.GROSS = model.GROSS;
                entity.GROSS_TIME = model.GROSS_TIME;
                entity.TARE = model.TARE;
                entity.TARE_TIME = model.TARE_TIME;
                entity.NET = model.NET;
                entity.BILL = model.BILL;
                entity.LOSS = model.LOSS;
                entity.TIME = model.TIME;
                entity.TRAIN_NUM = model.TRAIN_NUM;
                entity.USER_ID = model.USER_ID;
                entity.STATUS = model.STATUS;
                entity.STATE = model.STATE;



                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



        public virtual V_GDH_WEIGHING GetById(object id)
        {
            if (IsExists(id))
            {
                V_GDH_WEIGHING entity = m_Rep.GetById(id);
                V_GDH_WEIGHING model = new V_GDH_WEIGHING();
                model.WEIGHING_ID = entity.WEIGHING_ID;
                model.SERIAL_NUM = entity.SERIAL_NUM;
                model.CAR_NUM = entity.CAR_NUM;
                model.CAR_TYPE = entity.CAR_TYPE;
                model.GROSS = entity.GROSS;
                model.GROSS_TIME = entity.GROSS_TIME;
                model.TARE = entity.TARE;
                model.TARE_TIME = entity.TARE_TIME;
                model.NET = entity.NET;
                model.BILL = entity.BILL;
                model.LOSS = entity.LOSS;
                model.TIME = entity.TIME;
                model.TRAIN_NUM = entity.TRAIN_NUM;
                model.USER_ID = entity.USER_ID;
                model.STATUS = entity.STATUS;
                model.STATE = entity.STATE;

                return model;
            }
            else
            {
                return null;
            }
        }

        public virtual V_GDH_WEIGHING GetItem(Expression<Func<V_GDH_WEIGHING, bool>> whereLambda)
        {

            V_GDH_WEIGHING entity = m_Rep.GetItem(whereLambda);
            if (entity == null) return null;

            V_GDH_WEIGHING model = new V_GDH_WEIGHING();
            model.WEIGHING_ID = entity.WEIGHING_ID;
            model.SERIAL_NUM = entity.SERIAL_NUM;
            model.CAR_NUM = entity.CAR_NUM;
            model.CAR_TYPE = entity.CAR_TYPE;
            model.GROSS = entity.GROSS;
            model.GROSS_TIME = entity.GROSS_TIME;
            model.TARE = entity.TARE;
            model.TARE_TIME = entity.TARE_TIME;
            model.NET = entity.NET;
            model.BILL = entity.BILL;
            model.LOSS = entity.LOSS;
            model.TIME = entity.TIME;
            model.TRAIN_NUM = entity.TRAIN_NUM;
            model.USER_ID = entity.USER_ID;
            model.STATUS = entity.STATUS;
            model.STATE = entity.STATE;


            return model;
        }


        /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<V_GDH_WEIGHING> list, ref ValidationErrors errors)
        {

            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.WEIGHING_ID, "WEIGHING_ID");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.SERIAL_NUM, "SERIAL_NUM");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.CAR_NUM, "CAR_NUM");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.CAR_TYPE, "CAR_TYPE");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.GROSS, "GROSS");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.GROSS_TIME, "GROSS_TIME");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.TARE, "TARE");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.TARE_TIME, "TARE_TIME");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.NET, "NET");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.BILL, "BILL");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.LOSS, "LOSS");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.TIME, "TIME");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.TRAIN_NUM, "TRAIN_NUM");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.USER_ID, "USER_ID");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.STATUS, "STATUS");
            excelFile.AddMapping<V_GDH_WEIGHING>(x => x.STATE, "STATE");


            //SheetName
            var excelContent = excelFile.Worksheet<V_GDH_WEIGHING>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new V_GDH_WEIGHING();
                entity.WEIGHING_ID = row.WEIGHING_ID;
                entity.SERIAL_NUM = row.SERIAL_NUM;
                entity.CAR_NUM = row.CAR_NUM;
                entity.CAR_TYPE = row.CAR_TYPE;
                entity.GROSS = row.GROSS;
                entity.GROSS_TIME = row.GROSS_TIME;
                entity.TARE = row.TARE;
                entity.TARE_TIME = row.TARE_TIME;
                entity.NET = row.NET;
                entity.BILL = row.BILL;
                entity.LOSS = row.LOSS;
                entity.TIME = row.TIME;
                entity.TRAIN_NUM = row.TRAIN_NUM;
                entity.USER_ID = row.USER_ID;
                entity.STATUS = row.STATUS;
                entity.STATE = row.STATE;


                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<V_GDH_WEIGHING> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        V_GDH_WEIGHING entity = new V_GDH_WEIGHING();
                        entity.WEIGHING_ID = model.WEIGHING_ID;
                        entity.SERIAL_NUM = model.SERIAL_NUM;
                        entity.CAR_NUM = model.CAR_NUM;
                        entity.CAR_TYPE = model.CAR_TYPE;
                        entity.GROSS = model.GROSS;
                        entity.GROSS_TIME = model.GROSS_TIME;
                        entity.TARE = model.TARE;
                        entity.TARE_TIME = model.TARE_TIME;
                        entity.NET = model.NET;
                        entity.BILL = model.BILL;
                        entity.LOSS = model.LOSS;
                        entity.TIME = model.TIME;
                        entity.TRAIN_NUM = model.TRAIN_NUM;
                        entity.USER_ID = model.USER_ID;
                        entity.STATUS = model.STATUS;
                        entity.STATE = model.STATE;


                        db.V_GDH_WEIGHING.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
        public void Dispose()
        {

        }

    }
}
